package com.ipay.foreign.service.impl;

import com.ipay.foreign.dao.MerchantPayParameterDao;
import com.ipay.foreign.pojo.MerchantPayParameter;
import com.ipay.foreign.service.MerchantPayParameterService;
import com.ipay.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service("merchantPayParameterService")
public class MerchantPayParameterServiceImpl implements MerchantPayParameterService {
    @Autowired
    private MerchantPayParameterDao merchantPayParameterDao;


    @Override
    public MerchantPayParameter queryObject(String id) {
        return merchantPayParameterDao.queryObject(id);
    }

    @Override
    public void save(MerchantPayParameter merchantPayInfo) {
        if (merchantPayInfo != null && StringUtil.isNotEmpty(merchantPayInfo.getSponsorOrderNo())) {
            MerchantPayParameter merchantPayParameter = new MerchantPayParameter();
            merchantPayParameter.setSponsorOrderNo(merchantPayInfo.getSponsorOrderNo());
            List<MerchantPayParameter> merchantPayList = merchantPayParameterDao.queryList(merchantPayParameter);
            if (merchantPayList != null && merchantPayList.size() == 0) {
                merchantPayParameterDao.save(merchantPayInfo);
            } else if (merchantPayList != null && merchantPayList.size() == 1) {
                MerchantPayParameter merchantPayParameterOld = merchantPayList.get(0);
                merchantPayInfo.setId(merchantPayParameterOld.getId());
                merchantPayParameterDao.update(merchantPayInfo);
            }
        }else{
            merchantPayParameterDao.save(merchantPayInfo);
        }
    }

    @Override
    public void update(MerchantPayParameter merchantInfo) {
        merchantPayParameterDao.update(merchantInfo);
    }

    @Override
    public List<MerchantPayParameter> queryList(Map<String, Object> map) {
        return merchantPayParameterDao.queryList(map);
    }
}
